package com.bdqn.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.bdqn.entity.Module;
import com.bdqn.entity.Role;
import com.bdqn.mapper.ModuleMapper;
import com.bdqn.mapper.RoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/sys/role")

public class RoleController {
	
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private ModuleMapper moduleMapper;
	
	@RequestMapping("/findAllRole")
	public String findAllRole(HttpSession session,@RequestParam(value = "pageno", defaultValue = "1") int pageNo) {
		//fengyechanshu
		PageHelper.startPage(pageNo, 5);
		List<Role> listRole =roleMapper.getAllRole();
		PageInfo<Role> pageInfo = new PageInfo<>(listRole);
		//fengyexingxi
		session.setAttribute("pageInfo", pageInfo);
		
		
		return "redirect:/jsp/sys/role/roleList.jsp";
		
	}
   	@RequestMapping("/doUpdateRole")
   	public String doUpadateRole(Role role) {
   		
   		
   		int num=roleMapper.UpdateRole(role);
   	
   		return "redirect:/sys/role/findAllRole";
   	}
	//授权之前的查询
	@RequestMapping("/goRoleGrant")
	public String goRoleGrant(int rid,Model model) {
	List<Module> listRoleModule = moduleMapper.getAllModuleByRoleId(rid);
	
	List<Module> listModule = moduleMapper.getAllModule();
	
	model.addAttribute("mlist", listRoleModule);
	
	model.addAttribute("mclist", listModule);
	
		return "forward:/jsp/sys/role/roleGrant.jsp";
		
	}
	//删除
    @RequestMapping("/delRole")
	public String delRole(int role_id) {
	System.out.println(""+role_id);
	int num=roleMapper.delRole(role_id);
	System.out.println(""+role_id);
	System.out.println(num);
		return "redirect:/sys/role/findAllRole";
	}
    @RequestMapping("/addRole")
   	public String addEmp(Role role) {
   		int num=roleMapper.addRole(role);
   		if(num>0) {
   			System.out.println("添加成功");
   			return"redirect:/sys/role/findAllRole";
   		}else { 
   			System.out.println("添加失败");
   			return"redirect:/jsp/sys/role/roleAdd.jsp";
   		}
   		} 
       //授予权限
  
       public String roleGrant(int rid,String[] mids) {
      
       	System.out.println("执行R");
       	moduleMapper.delModuleRoleId(rid);
       	
       	List<String> mid=Arrays.asList(mids);
       	System.out.println("执行R");
       	moduleMapper.addModuleRole(rid,mid);
       	return"redirect:/login.jsp";
       }
       
   
   	@RequestMapping("/roleView")
   	public String roleView(int role_id, Model model) {
   		Role us=roleMapper.searchRoleById(role_id);
   		model.addAttribute("us", us);
   		return"forward:/jsp/sys/role/roleView.jsp";
   	}
  
   	@RequestMapping("/UpdateRole")
   	public String doUpdateRole(int role_id,Model model) {
   		
   		Role us=roleMapper.searchRoleById(role_id);
   		model.addAttribute("us", us);
   		return"forward:/jsp/sys/role/rolemodify.jsp";
   	}
   


}
